package com.infore.operation.controller;

import com.infore.operation.entity.*;
import com.infore.operation.exception.OperationException;
import com.infore.operation.service.StatisticsService;
import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.FormatUtil;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;


import com.infore.operation.utils.StatusCode;
import javafx.scene.control.ListCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:wk
 * @date:Created in 10:20 2018\4\17 0017
 */
@RestController
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    /**
     * 故障统计
     * @param userId
     * @param type
     * @param startTime
     * @param endTime
     * @param pageNum
     * @return
     */
    @RequestMapping("/servlet/GZTJ.do")
    public AbstractJsonObject queryFaultStatistics(@RequestParam(value = "userId")String userId,
                                                   @RequestParam(value = "type")String type,
                                                   @RequestParam(value = "startTime",required = false)String startTime,
                                                   @RequestParam(value = "endTime",required = false)String endTime,
                                                   @RequestParam(value = "pageNum",required = false)String pageNum) throws ParseException {
        try {
            if (startTime!=null) {
                FormatUtil.secondA.parse(startTime);
            }
            if (endTime!=null) {
                FormatUtil.secondA.parse(endTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误!（yyyy-MM-dd）");
        }
        List<Map<String,Object>> list = statisticsService.queryFaultStatistics(userId,type,startTime,endTime,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        if("1".equals(type)) {
            map.put("Fgsdata", list);
        }else if("2".equals(type)) {
            map.put("Brand", list);
        }else if("3".equals(type)) {
            map.put("Type", list);
        }else if("4".equals(type)) {
            map.put("Time", list);
        }else if("5".equals(type)) {
            map.put("Ptype", list);
        }else if("6".equals(type)) {
            map.put("Ctype", list);
        }else if("7".equals(type)) {
            map.put("Pson", list);
        }else if("8".equals(type)){
            map.put("Model", list);
        }else {
            throw new OperationException("type参数不在范围内");
        }
        return FormatUtil.queryListObject(map,"获取故障统计成功");
    }

    /**
     * 运营统计
     * @param userId
     * @param type
     * @param pageNum
     * @return
     */
    @RequestMapping("/servlet/YYTJ.do")
    public AbstractJsonObject queryOperationStatistics(@RequestParam(value = "userId")String userId,
                                           @RequestParam(value = "type")String type,
                                           @RequestParam(value = "pageNum",required = false)String pageNum)  throws ParseException{
        List<Map<String,Object>> list = statisticsService.queryOperationStatistics(userId,type,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        if("1".equals(type)){
            map.put("Fgsdata", list);
        }else if("2".equals(type)){
            map.put("Brand", list);
        }else if("3".equals(type)){
            map.put("Ptype", list);
        }else if("4".equals(type)){
            map.put("Time", list);
        }else if("5".equals(type)){
            map.put("Ctype", list);
        }else if("6".equals(type)){
            map.put("Pson", list);
        }else {
            throw new OperationException("type参数不在范围内");
        }
        return FormatUtil.queryListObject(map,"获取运营统计成功");
    }

    /**
     * 查询质量反馈列表
     * @param userId
     * @param stationId
     * @param pageNum
     * @return
     */
    @RequestMapping("/servlet/queryQualityList.do")
    public AbstractJsonObject queryQualityList(@RequestParam(value = "userId")String userId,
                                   @RequestParam(value = "stationId",required = false)String stationId,
                                   @RequestParam(value = "pageNum")Integer pageNum){
        List<QualityList> lists = statisticsService.queryQualityList(userId,stationId,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", lists);
        return FormatUtil.queryListObject(map,"获取质量反馈列表成功");
    }

    /**
     * 获取质量反馈信息详情
     * @param qualityId
     * @return
     */
    @RequestMapping("/servlet/queryQualityMessage.do")
    public AbstractJsonObject queryQualityMessage(@RequestParam(value = "qualityId")String qualityId){
        Quality quality = statisticsService.queryQualityMessage(qualityId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bean", quality);
        return FormatUtil.queryListObject(map,"获取质量反馈信息详情成功");
    }
    /**
     * 质量反馈上报
     * @param userId
     * @param time
     * @param equipId
     * @param num
     * @param faultDisc
     * @param discrib
     * @param artisanId
     * @param equipName
     * @param receiveTime
     * @param images
     * @return
     */
    @RequestMapping("/servlet/Quality.do")
    public AbstractJsonObject Quality(@RequestParam(value = "userId")String userId,
                          @RequestParam(value = "time")String time,
                          @RequestParam(value = "equipId")String equipId,
                          @RequestParam(value = "num")String num,
                          @RequestParam(value = "faultDisc")String faultDisc,
                          @RequestParam(value = "discrib")String discrib,
                          @RequestParam(value = "artisanId",required = false)String artisanId,
                          @RequestParam(value = "equipName")String equipName,
                          @RequestParam(value = "receiveTime",required = false)String receiveTime,
                          @RequestParam(value = "picture",required = false)MultipartFile[] images){
        try {
            FormatUtil.secondG.parse(time);
            if (receiveTime!=null){
                FormatUtil.secondG.parse(receiveTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd HH:mm:ss）");
        }
        try {
            statisticsService.saveQuality(userId,time,equipId,num,faultDisc,discrib,artisanId,equipName,receiveTime,images);
        } catch (IOException e) {
            e.printStackTrace();
            return  FormatUtil.queryAbstractJsonObject("质量反馈上报失败", StatusCode.CODE_ERROR);
        }
        return  FormatUtil.queryAbstractJsonObject("质量反馈上报成功", StatusCode.CODE_SUCCESS);
    }

    /**
     * 获取质量反馈列表
     * @param userId
     * @param state
     * @param pageNum
     * @return
     */
    @RequestMapping("/servlet/queryQualityArtisanSqeList.do")
    public AbstractJsonObject queryQualityArtisanSqeList(@RequestParam(value = "userId")String userId,
                                             @RequestParam(value = "state")String state,
                                             @RequestParam(value = "pageNum")Integer pageNum){
        List<NQualityList> list = statisticsService.queryQualityArtisanSqeList(userId,state,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取质量反馈列表成功");
    }

    /**
     * 质量反馈技术审核
     * @param suggestion
     * @param time
     * @param id
     * @param sqeId
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/queryQualityArtisanUp.do")
    public AbstractJsonObject queryQualityArtisanUp(@RequestParam(value = "suggestion")String suggestion,
                                        @RequestParam(value = "time")String time,
                                        @RequestParam(value = "id")String id,
                                        @RequestParam(value = "sqeId",required = false)String sqeId,
                                        @RequestParam(value = "userId")String userId){
        try {
            FormatUtil.secondG.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd HH:mm:ss）");
        }
        statisticsService.updateQualityArtisan(suggestion,time,id,sqeId,userId);
        return  FormatUtil.queryAbstractJsonObject("质量反馈技术审核成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 质量反馈品质审核
     * @param scheme
     * @param measures
     * @param causes
     * @param id
     * @param time
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/queryQualitySqeUp.do")
    public AbstractJsonObject queryQualitySqeUp(@RequestParam(value = "scheme")String scheme,
                                    @RequestParam(value = "measures")String measures,
                                    @RequestParam(value = "causes")String causes,
                                    @RequestParam(value = "id")String id,
                                    @RequestParam(value = "time")String time,
                                    @RequestParam(value = "userId")String userId){
        try {
            FormatUtil.secondG.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd HH:mm:ss）");
        }
        statisticsService.updateQualitySqe(scheme,measures,causes,id,time,userId);
        return  FormatUtil.queryAbstractJsonObject("质量反馈品质审核成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 建议类型
     * @return
     */
    @RequestMapping("/servlet/querySuggestionType.do")
    public AbstractJsonObject querySuggestionType() {
       List<SuggestionType> list= statisticsService.querySuggestionType();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("aspect", list);
        return FormatUtil.queryListObject(map,"获取建议类型成功");
    }

    /**
     *  建议上报
     * @param type
     * @param anonymity
     * @param aspectOn
     * @param submitTime
     * @param suggestion
     * @param userId
     * @param title
     * @return
     */
    @RequestMapping("/servlet/querySuggestion.do")
    public AbstractJsonObject querySuggestion( @RequestParam(value = "type")String type,
                                   @RequestParam(value = "anonymity")String anonymity,
                                   @RequestParam(value = "aspectOn")String aspectOn,
                                   @RequestParam(value = "submitTime",required = false)String submitTime,
                                   @RequestParam(value = "suggestion")String suggestion,
                                   @RequestParam(value = "userId")String userId,
                                   @RequestParam(value = "title")String title){
        try {
            if (submitTime!=null) {
                FormatUtil.secondG.parse(submitTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd HH:mm:ss）");
        }
        statisticsService.saveSuggestion(type,anonymity,aspectOn,submitTime,suggestion,userId,title);
        return  FormatUtil.queryAbstractJsonObject("建议上报成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 获取建议列表
     * @param pageNum
     * @param suggestionState
     * @param sortType
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/querySuggestionList.do")
    public AbstractJsonObject querySuggestionList(@RequestParam(value = "pageNum")Integer pageNum,
                                      @RequestParam(value = "suggestionState")String suggestionState,
                                      @RequestParam(value = "sortType")String sortType,
                                      @RequestParam(value = "userId")String userId){
        List<Suggestion> list = statisticsService.querySuggestionList(pageNum,suggestionState,sortType,userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取建议列表成功");
    }

    /**
     * 赞建议
     * @param suggestionId
     * @param userId
     * @param supportState
     * @return
     */
    @RequestMapping("/servlet/querySupport.do")
    public AbstractJsonObject querySupport( @RequestParam(value = "suggestionId")String suggestionId,
                                @RequestParam(value = "userId")String userId,
                                @RequestParam(value = "supportState")String supportState){
        statisticsService.querySupport(suggestionId,userId,supportState);
        return  FormatUtil.queryAbstractJsonObject("成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 查询建议
     * @param pageNum
     * @param sortType
     * @param startTime
     * @param endTime
     * @param suggestionType
     * @param keyword
     * @param branchId
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/searchSuggestion.do")
    public AbstractJsonObject searchSuggestion(@RequestParam(value = "pageNum")Integer pageNum,
                                   @RequestParam(value = "sortType")String sortType,
                                   @RequestParam(value = "startTime")String startTime,
                                   @RequestParam(value = "endTime")String endTime,
                                   @RequestParam(value = "suggestionType")String suggestionType,
                                   @RequestParam(value = "keyword")String keyword,
                                   @RequestParam(value = "branchId")String branchId,
                                   @RequestParam(value = "userId")String userId){
        try {
            FormatUtil.secondA.parse(startTime);
            FormatUtil.secondA.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd）");
        }
        List<Suggestion> list = statisticsService.searchSuggestion(pageNum,sortType,startTime,endTime,suggestionType,keyword,branchId,userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"查询建议成功");
    }
}
